import router from ".";
import NProgress from 'nprogress'
import { useUserStore } from "@/stores/user";
import { useNoticeStore } from "@/stores/notice";
import { ElMessage } from "element-plus";
NProgress.configure({ showSpinner: false })
const whiteList = ['/login']
router.beforeEach(async (to, from, next) => {
    NProgress.start()
    document.title = to.meta.title as string || ''
    const userStore = useUserStore()
    if (userStore.token) {
        try {
            if (!userStore.user.username) {
                await userStore.getUserInfo()

                next({ ...to, replace: true });
            } else if (whiteList.includes(to.path)) {
                next('/')
            } else {
                next()
            }
        } catch (err) {
            userStore.token = ''
            next({ path: "/login", query: { redirect: to.path } });
        }
    } else {
        if (whiteList.includes(to.path)) {
            next()
        } else {
            next({ path: '/login' })
        }
    }

})

router.afterEach(() => {
    NProgress.done()
})